var _ = require("./Underscore.js");
var executeRecord = {};
executeRecord.STORAGE_KEY = "EXECUTE_RECORD";
var executeRecordStorage = storages.create(executeRecord.STORAGE_KEY);


//持久化
executeRecord._persistent = function(config){
    executeRecordStorage.put(executeRecord.STORAGE_KEY, JSON.stringify(config));
}
//get
executeRecord._get = function(){
    return executeRecordStorage.get(executeRecord.STORAGE_KEY);
}
executeRecord.getConfig = function(){
    if(executeRecord._get()){
        return JSON.parse(executeRecord._get());
    }
    return null;
}



//开始执行
executeRecord.startExecuteRecord = function(apps){
    executeRecord._persistent({
        //开始时间
        start:java.lang.System.currentTimeMillis(),
        //结束时间
        end:-1,
        //运行的app
        apps:apps,
        //执行中的app
        executeIng:null,
        //错误的app
        error:[],
        //重复完成的app
        repeat:[],
        //正常完成的app
        finish:[]
        
    });
}
//app开始执行 
executeRecord.startAppRecord = function(appName){
    var config = executeRecord._get();
    config = JSON.parse(config);
    config.executeIng = {
        start:java.lang.System.currentTimeMillis(),
        end:-1,
        appName:appName,
        progress:0,
        errorMsg:"",
    }
    executeRecord._persistent(config);
}
//app更新进度
executeRecord.updateAppProgress = function(progress){
    var config = executeRecord._get();
    config = JSON.parse(config);
    config.executeIng.progress = progress;
    executeRecord._persistent(config);
}
//app异常结束
executeRecord.endAppRecordWithError = function(errorMsg){
    var config = executeRecord._get();
    config = JSON.parse(config);
    config.executeIng.errorMsg = errorMsg;
    config.executeIng.end = java.lang.System.currentTimeMillis();
    config.error.push(config.executeIng);
    executeRecord._persistent(config);
}
//app重复执行结束
executeRecord.endAppRecordWithRepeat = function(){
    var config = executeRecord._get();
    config = JSON.parse(config);
    config.executeIng.end = java.lang.System.currentTimeMillis();
    config.repeat.push(config.executeIng);
    executeRecord._persistent(config);
}
//app正常结束
executeRecord.endAppRecordWithFinish = function(){
    var config = executeRecord._get();
    config = JSON.parse(config);
    config.executeIng.end = java.lang.System.currentTimeMillis();
    config.finish.push(config.executeIng);
    executeRecord._persistent(config);
}

//执行结束
executeRecord.endExecuteRecord = function(){
    var config = executeRecord._get();
    config = JSON.parse(config);
    config.end = java.lang.System.currentTimeMillis();
    executeRecord._persistent(config);
}


module.exports = executeRecord;